jQ(function(){
	
	initCCUserInfo();
	
	jQ("#viewopt").dialog({
		modal: true,
		bgiframe: true,
		autoOpen: false,
		closeOnEscape:false,
		draggable:false,
		height:200,
		buttons: {
			
			"确定": function() {
				var opt=jQ("#viewopt");
				var triggerfun=opt.data("function");
				opt.trigger(triggerfun);
				opt.removeData("function");
				jQ("#viewopt_p").hide();
				jQ("#viewopt_img").show();
				//activeBtnOnDialog("viewopt",false);
				//jQ(this).dialog('close');
			},
			"取消": function() {
				var viewopt=jQ("#viewopt");
				var triggerfun=jQ("#viewopt").data("function");
				viewopt.unbind(triggerfun);
				viewopt.find("input.eventid").val("");
				viewopt.removeData("function");
				jQ(this).dialog('close');
			}
		},
		close: function() {
			jQ("#viewopt_p").show();
			jQ("#viewopt_img").hide();
		},
		open:function(){
		}
	});
});


function initCCUserInfo(){
	jQ.ajax({
		type:"POST",
		url:"controlAjax!userInfo.action",
		success:function(userInfo){
			if(userInfo!=null){
				dataObj=eval("("+userInfo+")");
				if(dataObj['loginflag']){
					jQ("div.info_title span").eq(1).empty().append(dataObj['email']);
					setValue(0,dataObj['name']);
					setValue(1,dataObj['blog']);
					setValue(2,dataObj['job']);
					
					setMobileVale(dataObj.mobilenum);
					jQ.ajax({
						type:"POST",
						url:"controlAjax!userEmail.action",
						success:function(data){
							var contentHtml="";
							var tds=jQ("table.information td").eq(1).empty();
							jQ(data).find('email').each(function(){
								var email=jQ(this).find("Email").text();
								if(email==dataObj['email']){
									contentHtml=createEmailContentHtml(email,parseInt(jQ(this).find("Status").text(),10),jQ(this).find("Description").text(),1,false)
									tds.append(contentHtml);
								}else{//绑定邮箱
									if(parseInt(jQ(this).find("Bindable").text(),10)==1){
										contentHtml=createEmailContentHtml(email,parseInt(jQ(this).find("Status").text(),10),jQ(this).find("Description").text(),2,false)
										tds.append(contentHtml);
									}else{
										contentHtml=createEmailContentHtml(email,parseInt(jQ(this).find("Status").text(),10),jQ(this).find("Description").text(),3,false)
										tds.append(contentHtml);
									}
								}
							});
							tds.append(createEmailContentHtml("",0,"",4,true));
						}
					});
				}
			}
		}
	});
}

function setValue(index,value){
	if((typeof parseInt(index,10))=="number")
		jQ("#ccinfo_ul li").eq(index).find("span").eq(1).empty().append(value);
}

function createEmailContentHtml(email,flag,description,type,editflag){
	description=HTMLEnCode(description);
	var contentHtml="";
	contentHtml+="<fieldset class=\"add_mail\">";
	if(type==1)
		contentHtml+="<legend>注册邮箱</legend>";
	else if(type==2)
		contentHtml+="<legend>绑定邮箱</legend>";
	else if(type==3)
		contentHtml+="<legend>未验证邮箱</legend>";
	else if(type==4)
		contentHtml+="<legend>添加新邮箱</legend>";
	contentHtml+="<ul>";
	contentHtml+="<li>";
	contentHtml+="<span class=\"info_t\">描述：</span>";
	if(editflag)
		contentHtml+="<input type=\"text\" value=\""+(description.length>=10?description.substr(0,10):description)+"\"/>";
	else
		contentHtml+="<span class=\"describe\" title=\""+description+"\">"+(description.length>=10?description.substr(0,10):description)+"</span>";
	contentHtml+="</li>";
	
	contentHtml+="<li>";
	contentHtml+="<span class=\"info_t\">邮箱：</span>";
	if(editflag)
		contentHtml+="<input type=\"text\" value=\""+email+"\"/>";
	else
		contentHtml+="<span class=\"describe\" title=\""+email+"\">"+email+"</span>";
	contentHtml+="</li>";
	
	
	contentHtml+="<li>";
	contentHtml+="<span class=\"info_t\" cid=\""+flag+"\">&nbsp;</span>";
	if(type==3){
		contentHtml+="<span>验证中…</span>";
		contentHtml+="<a class=\"wrap\" onclick=\"resendEmail(this)\">重新发送验证</a>";
	}else if(type==4){
		contentHtml+="";
	}else{
		if(editflag){
			if(flag==1)
				contentHtml+="<input type=\"checkbox\" checked=\"checked\"/>";
			else
				contentHtml+="<input type=\"checkbox\" />";
		}else{
			if(flag==1)
				contentHtml+="<input type=\"checkbox\" disabled=\"disabled\" checked=\"checked\"/>";
			else
				contentHtml+="<input type=\"checkbox\" disabled=\"disabled\"/>";
		}
		contentHtml+="<span>设置为接收邮箱</span>";
	}
	contentHtml+="</li>";
	
	
	contentHtml+="<li>";
	if(type==1){
		contentHtml+="<a onclick=\"editEmail(this)\">编辑</a>";
	}else if(type==2||type==3){
		contentHtml+="<a onclick=\"deleteEmail(this)\">删除</a>";
		contentHtml+="<a onclick=\"editEmail(this)\">编辑</a>";
	}else if(type==4){
		contentHtml+="<a onclick=\"addEmail(this);\">添加</a>";
	}
	
	contentHtml+="</li>";
	contentHtml+="</ul>";
	contentHtml+="</fieldset>";
	return contentHtml;
}

var dataObj;
function mail_edit(obj){
	jQ(obj).parents('ul').find('input.mail').toggle();
	jQ(obj).parents('ul').find('span.describe').toggle();
	jQ(obj).parents('li').find('a:eq(0)').nextAll().toggle();
}

function canceledit(){
	setValue(0,dataObj['name']);
	setValue(2,dataObj['job']);
	setValue(1,dataObj['blog']);
	jQ("span.fix a").show();
	jQ("div.edit").hide();
}

function toedit(obj){
	setValue(1,"<input type='text' style='width:150px;' id='blogipt' value='"+dataObj['blog']+"'>");
	setValue(2,"<textarea id='jobipt' style='width:150px;'>"+dataObj['job']+"</textarea>");
	jQ(obj).hide();
	jQ("div.edit").show();
}

function okedit(){
	var blog=jQ("#blogipt").val();
	var strRegex = "^(http://)+(([0-9a-zA-Z_]+[\.]+[0-9a-zA-Z_])?([0-9a-zA-Z_]))+[\.]+([a-zA-Z]{2,3})+.+$";
	var re = new RegExp(strRegex);
	if(blog.length>0&&!re.test(blog)){
		clip_tips("个人网站格式错误 请输入以http://开头的URL");
		return;
	}
	var job=jQ.trim(jQ("#jobipt").val());
	if(job.length>100){
		clip_tips("简介不能大于100字");
		return;
	}
	jQ.ajax({
		type:"POST",
		url:"controlAjax!saveUserInfo.action",
		data:{
			jobInput:job,
			blogInput:blog
		},
		success:function(data){
			if(data!=null){
				dataObj=eval("("+data+")");
				if(dataObj['flag']){
					initUserInfo();
					checkExistUser();
					jQ("span.fix a").show();
					jQ("div.edit").hide();
				}
				clip_tips(dataObj['result']);
			}
		}
	});
}

function editEmail(obj){
	var li=jQ(obj).parent().siblings();
	var description = li.eq(0).find("span").eq(1).attr("title");
	var email = li.eq(1).find("span").eq(1).text();
	var status = li.eq(2).find("input");
	if(email.length>0){
		li.eq(0).find("span").eq(1).empty().append("<input type=\"text\" value=\""+description+"\"/>");
		li.eq(1).find("span").eq(1).empty().append("<input type=\"text\" disabled=\"disabled\" value=\""+email+"\"/>");
		jQ(status).removeAttr("disabled");
		var pageHtml="";
		if(email!=dataObj['email'])
			pageHtml+="<a onclick=\"deleteEmail(this)\">删除</a>";
		pageHtml+="<a onclick=\"cancelEmail(this)\">取消</a><a onclick=\"saveEmail(this)\">确定</a>";
		jQ(obj).parent().empty().append(pageHtml);
	}
}
function cancelEmail(obj){
	var li=jQ(obj).parent().siblings();
	var description = li.eq(0).find("span").eq(1);
	var email = li.eq(1).find("span").eq(1);
	var status = li.eq(2).find("input");
	
	description.empty().append(description.attr("title").length>=10?description.attr("title").substr(0,10):description.attr("title"));
	email.empty().append(email.attr("title"));
	jQ(status).attr("disabled","disabled");
	var cid=jQ(status).parent().find("span").attr("cid");
	jQ(status).attr("checked",(cid=="0"?false:true));
	var pageHtml="";
	if(email.attr("title")!=dataObj['email'])
		pageHtml+="<a onclick=\"deleteEmail(this)\">删除</a>";
	pageHtml+="<a onclick=\"editEmail(this)\">编辑</a>";
	jQ(obj).parent().empty().append(pageHtml);
}
function deleteEmail(obj){
	var li=jQ(obj).parent().siblings();
	var email = li.eq(1).find("span").eq(1);
	if(email.attr("title")==(email.text()||email.find("input").val())){
		if(email.text()==dataObj['email']){
			clip_tips("不能删除");
			return;
		}else{
			jalert("您确定删除"+email.attr("title")+"这个邮箱吗？",{emailInput:email.attr("title")}, deleteEmail_callback);
		}
	}
}

function deleteEmail_callback(event){
	jQ.ajax({
		type:"POST",
		url:"controlAjax!deleteEmail.action",
		data:{
			emailInput:event.data.emailInput
		},
		success:function(data){
			if(data!=null){
				dataObj=eval("("+data+")");
				if(dataObj['flag'])
					initEmailInfo();
				clip_tips(dataObj['result']);
			}
		}
	});
}

function saveEmail(obj){
	var li=jQ(obj).parent().siblings();
	var description=li.eq(0).find("span").eq(1).find("input").val();
	description = jQ.trim(description);
	var email=li.eq(1).find("span").eq(1).find("input").val();
	email = jQ.trim(email);
	var status=li.eq(2).find("input").attr("checked");
	status=(status==true?1:0);
	var p = /^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.){1,4}[a-z]{2,3}$/i;
	if(!p.test(email)){
		clip_tips("Email错误");
		return;
	}
	if(typeof status!="number")
		return;
	if(description.length<=0||description.length>20){
		clip_tips("描述错误");
		return;
	}
	jQ.ajax({
		type:"POST",
		url:"controlAjax!saveEmail.action",
		data:{
			emailInput:email,
			descInput:description,
			status:status
		},
		success:function(data){
			if(data!=null){
				dataObj=eval("("+data+")");
				if(dataObj['flag'])
					initEmailInfo();
				clip_tips(dataObj['result']);
			}
		}
	});
}
function addEmail(obj){
	var li=jQ(obj).parent().siblings();
	var description = li.eq(0).find("input").val();
	var email = li.eq(1).find("input").val();
	email = jQ.trim(email);
	description = jQ.trim(description);
	var p = /^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.){1,4}[a-z]{2,3}$/i;
	if(!p.test(email)){
		clip_tips("Email错误");
		return;
	}
	if(description.length<=0||description.length>20){
		clip_tips("描述错误");
		return;
	}
	jQ.ajax({
		type:"POST",
		url:"controlAjax!bindEmail.action",
		data:{
			emailInput:email,
			descInput:description
		},
		success:function(data){
			if(data!=null){
				dataObj=eval("("+data+")");
				clip_tips(dataObj['result']);
				initEmailInfo();
			}
		}
	});
}
function resendEmail(obj){
	var li=jQ(obj).parent().siblings();
	var description = li.eq(0).find("span.describe").text();
	var email = li.eq(1).find("span.describe").text();
	email = jQ.trim(email);
	description = jQ.trim(description);
	var p = /^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.){1,4}[a-z]{2,3}$/i;
	if(!p.test(email)){
		clip_tips("Email错误");
		return;
	}
	if(description.length<=0){
		clip_tips("描述错误");
		return;
	}
	jQ.ajax({
		type:"POST",
		url:"controlAjax!resendEmail.action",
		data:{
			emailInput:email,
			descInput:description
		},
		success:function(data){
			if(data!=null){
				dataObj=eval("("+data+")");
				clip_tips(dataObj['result']);
			}
		}
	});
}

function setMobileVale(mobilenum){
	if(jQ.trim(mobilenum).length==0){
		jQ("#modelchangebtn").hide();
		jQ(".editmode").show();
		jQ("#mobilenumipt").css({border:"1px solid #7f9db9"});
		jQ("#submitinfo").empty().hide();
		jQ("#mobilenumipt").attr("disabled","");
		
	}else{
		jQ("#orientmobilenum").val(mobilenum);
		jQ("#mobilenumipt").val(mobilenum);
		jQ("#modelchangebtn").show();
		jQ("#mobilenumipt").attr("disabled","disabled");
	}
	jQ("#sendCodebtn").unbind("click").bind("click",sendVerCodeConfirm);
}

function editMobile(obj){
	if(jQ(obj).hasClass("editon")){
		jQ(obj).removeClass("editon").text("修改");
		jQ(".editmode").hide();
		jQ("#mobilenumipt").css({border:"0px"});
		jQ("#mobilenumipt").attr("disabled","disabled").val(jQ("#orientmobilenum").val());
	}else{
		jQ("#mobilenumipt").attr("disabled","");
		jQ(obj).addClass("editon").text("取消");
		jQ(".editmode").show();
		jQ("#mobilenumipt").css({border:"1px solid #7f9db9"});
		jQ("#submitinfo").empty().hide();
	}
}

function sendVerCodeConfirm(){
	if(jQ("#modelchangebtn").css("display")!='none'){
		var oldnum=jQ("#orientmobilenum").val();
		var minput=jQ.trim(jQ("#mobilenumipt").val());
		if(!minput.match(mobilever)){
			showAjaxCallBackMsg(0,"请输入正确的手机号码。");
			return;
		}
		if(minput==oldnum){
			showAjaxCallBackMsg(0,"对不起，您输入的手机号已经绑定在您的账户上。");
			return;
		}
		var opt=jQ("#viewopt");
		opt.bind("sendVerCodeEvent",sendVerCode);
		opt.data("function","sendVerCodeEvent")
		jQ("#viewopt_p").empty().append("您确定要绑定新的手机号么？");
		opt.dialog( "option", "title","提醒" );
		opt.dialog("open");
	}else{
		sendVerCode(true);
	}
}

var mobilever = new RegExp(/((1(30|31|32|33|34|35|36|37|38|39|45|47|50|51|52|53|54|55|56|57|58|59|80|81|82|83|84|85|86|87|88|89)\d{8,8})|((106|0)\d{10,11}))/img);
var mobilecode=new RegExp(/(\d{6})/img);

function resetVerCode(){
	jQ("#sendCodebtn").text("发送验证").unbind("click").bind("click",sendVerCodeConfirm);
	jQ("#mobilenumipt").attr("disabled","").css({border:"1px solid #7f9db9"});
	jQ("#verifyCodebtn").unbind("click");
}

function sendVerCode(flag){
	var minput=jQ.trim(jQ("#mobilenumipt").val());
	if(flag){
		if(!minput.match(mobilever)){
			showAjaxCallBackMsg(0,"请输入正确的手机号码。");
			return;
		}
	}
	jQ.ajax({
		type:"POST",
		url:"userAjax!sendVerCodeAndReset.action",
		data:{
			mobilenum:minput
		},
		success:function(data){
			jQ("#viewopt").dialog("close");
			var dataObj=eval("("+data+")");
			showAjaxCallBackMsg(eval(dataObj.flag),dataObj.result);
			if(dataObj.flag){
				jQ("#sendCodebtn").text("重新发送");
				jQ("#mobilenumipt").css({border:"0px"}).attr("disabled","disabled");
				jQ("#sendCodebtn").unbind("click").bind("click",resetVerCode);
				jQ("#orientmobilenum").val("");
				jQ("#verifyCodebtn").unbind("click").bind("click",verCode);
			}
		}
	});
}

function verCode(obj){
	var minput=jQ.trim(jQ("#veript").val());
	if(!minput.match(mobilecode)){
		showAjaxCallBackMsg(0,"验证码只可能是6个数字。");
		return;
	}else{
		jQ.ajax({
			type:"POST",
			url:"userAjax!verifyCode.action",
			data:{
				codenum:minput
			},
			success:function(data){
				var dataObj=eval("("+data+")");
				showAjaxCallBackMsg(eval(dataObj.flag),dataObj.result);
				if(dataObj.flag){
					jQ("#modelchangebtn").removeClass("editon").text("修改");
					jQ(".editmode").hide();
					jQ("#verifyCodebtn").unbind("click");
					setTimeout(function(){
						jQ("#submitinfo").fadeOut();
					},2000);
				}
			}
		});
		
	}
}

function showAjaxCallBackMsg(flag,msg){
	if(flag){
		jQ("#submitinfo").removeClass("info").removeClass("error").addClass("info");
	}else{
		jQ("#submitinfo").removeClass("info").removeClass("error").addClass("error");
	}	
	jQ("#submitinfo").empty().append(msg).fadeIn();
}